<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" :rules="queryForm" v-show="showSearch" label-width="68px">
      <el-form-item label="违规车型" prop="violationTypecar">
        <el-input
          v-model="queryParams.violationTypecar"
          placeholder="请输入违规车型"
          clearable
          size="small"
          maxlength="30"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="违规车牌" prop="violationLicense">
        <el-input
          v-model="queryParams.violationLicense"
          placeholder="请输入违规车牌"
          clearable
          size="small"
          maxlength="10"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="违规说明" prop="violationExplain">
        <el-input
          v-model="queryParams.violationExplain"
          placeholder="请输入违规说明"
          clearable
          size="small"
          maxlength="50"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['server:violation:add']"-->
<!--        >新增-->
<!--        </el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['server:violation:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['server:violation:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="violationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index" align="center" prop="violationId"/>
      <el-table-column label="违规车型" align="center" prop="violationTypecar"/>
      <el-table-column label="违规车牌" align="center" prop="violationLicense"/>
      <el-table-column label="违规车标" align="center" prop="violationLogocar"/>
      <el-table-column label="违规说明" align="center" prop="violationExplain"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['server:violation:edit']"-->
<!--          >修改-->
<!--          </el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['server:violation:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改交通违规信息对话框 -->
<!--    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>-->
<!--      <el-form ref="form" :model="form" :rules="rules" label-width="80px">-->
<!--        <el-form-item label="违规车型" prop="violationTypecar">-->
<!--          <el-input v-model="form.violationTypecar" placeholder="请输入违规车型"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="违规车牌" prop="violationLicense">-->
<!--          <el-input v-model="form.violationLicense" placeholder="请输入违规车牌"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="违规车标" prop="violationLogocar">-->
<!--          <el-input v-model="form.violationLogocar" placeholder="请输入违规车标"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="违规说明" prop="violationExplain">-->
<!--          <el-input v-model="form.violationExplain" placeholder="请输入违规说明"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
<!--        <el-button @click="cancel">取 消</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->
  </div>
</template>

<script>
  import {
    listViolation,
    getViolation,
    delViolation,
    addViolation,
    updateViolation,
    exportViolation
  } from '@/api/server/traffic/violation'

  export default {
    name: 'Violation',
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 交通违规信息表格数据
        violationList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          violationTypecar: null,
          violationLicense: null,
          violationLogocar: null,
          violationExplain: null
        },
        // 表单参数
        form: {}
        ,
        queryForm: {
          violationTypecar: [{ pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: '不能为特殊字符', trigger: 'blur'}],
          violationLicense: [{ pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: '不能为特殊字符', trigger: 'blur'}],
        },
        // 表单校验
        rules: {
          // violationLicense:[{
          //   require:true,
          //   pattern:/^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/,
          //   message:'请输入正确的车牌号',
          //   trigger:'blur'
          // }],
          // violationTypecar: [
          //   {required: true, message: '违规车型不能为空', trigger: 'blur'}
          // ],
          // violationLogocar: [
          //   {required: true, message: '违规车标不能为空', trigger: 'blur'}
          // ],
          // violationExplain: [
          //   {required: true, message: '违规说明不能为空', trigger: 'blur'}
          // ],
        }
      }

    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询交通违规信息列表 */
      getList() {
        this.loading = true
        listViolation(this.queryParams).then(response => {
          this.violationList = response.rows
          this.total = response.total
          this.loading = false
        })

      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          violationId: null,
          violationTypecar: null,
          violationLicense: null,
          violationLogocar: null,
          violationExplain: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        }

        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.$refs['queryForm'].validate(valid => {
          if (valid) {
            this.getList()
          }
        })
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.violationId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加交通违规信息'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const violationId =
          row.violationId || this.ids
        getViolation(violationId).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改交通违规信息'
        })

      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
            if (valid) {
              if (this.form.violationId != null) {
                updateViolation(this.form).then(response => {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.getList()
                })

              } else {
                addViolation(this.form).then(response => {
                  this.msgSuccess('新增成功')
                  this.open = false
                  this.getList()
                })

              }
            }
          }
        )

      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const violationIds = row.violationId || this.ids
        this.$confirm('是否确认删除该数据项?', '警告', {
          cancelButtonClass: 'btn-custom-cancel',
          closeOnClickModal: false,
          type: 'warning'
        }).then(function() {
          return delViolation(violationIds)
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有交通违规信息数据项?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'info'
        }).then(function() {
          return exportViolation(queryParams)
        }).then(response => {
          this.download(response.msg)
        })
      }
    }
  }
</script>
